package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.squareup.picasso.Picasso;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.base.BaseStoreHouseRefreshRecyclerFragment;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.TaskAllInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by mapple on 17/3/1.
 * <p>
 * 待确认
 */

public class AllFragment extends BaseStoreHouseRefreshRecyclerFragment {

    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);

    private List<TaskAllInfo> infoList;

    @Override
    protected RecyclerBaseAdapter getListAdapter() {
        return new TaskAllAdapter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageSize = Integer.MAX_VALUE;

    }


    @Override
    protected void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        TaskAllInfo info = (TaskAllInfo) mAdapter.getData().get(position);
        Bundle args = new Bundle();
        args.putSerializable("info", info);
        args.putString("state", "allTask");
        if (info.getTypeName().equals("手机淘宝任务")) {
            UIHelper.showMpTaoBaoTask2(mActivity, args);
        } else if (info.getTypeName().equals("手机天猫任务")) {
            UIHelper.showMpTMTask2(mActivity, args);
        } else if (info.getTypeName().equals("电脑淘宝任务")) {
            UIHelper.showDnTaoBaoTask2(mActivity, args);
        }else if (info.getTypeName().equals("特别任务")) {
            UIHelper.showDianFuTbTask2(mActivity, args);
        }
        finish();
    }

    @Override
    protected void requestData(boolean refresh) {
        super.requestData(refresh);
        ApiHttpClient.receivedTask(this, userInfo.getWebzzCmsBuyerInfo().getUserid(), "001", "5",
                new JsonCallback<TaskAllInfo>(TaskAllInfo.class) {

                    @Override
                    public void onError(Call call, Exception e) {
                        onRequestError();
                    }

                    @Override
                    public void onResponse(
                            ResponseInfo<TaskAllInfo> response
                    ) {
                        int code = response.getCode();
                        if (code == 1) {
                            mErrorLayout.setErrorType(
                                    EmptyLayout.NETWORK_ERROR);
                            return;
                        }
                        infoList = response.getData();
                        if (infoList!=null) {
                            Collections.sort(infoList,
                                             new Comparator<TaskAllInfo>() {
                                                 @Override
                                                 public int compare(
                                                         TaskAllInfo taskAllInfo,
                                                         TaskAllInfo t1
                                                 ) {
                                                     return (t1
                                                             .getEndTime())
                                                             .compareTo(
                                                                     (taskAllInfo.getEndTime()));
                                                 }
                                             }
                            );
                        }
                        onRequestResponse(infoList);
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                        onRequestFinish();
                    }
                }
        );
    }

    public class TaskAllAdapter extends RecyclerBaseAdapter {


        @Override
        protected View onCreateItemView(ViewGroup parent, int viewType) {
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_all_fragment,
                    parent, false
            );
        }

        @Override
        protected ViewHolder onCreateItemViewHolder(
                View view, int viewType
        ) {
            return new ViewHolder(viewType, view);
        }

        @Override
        public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
            super.onBindItemViewHolder(holder, position);
            final ViewHolder vh = (ViewHolder) holder;
            TaskAllInfo info = (TaskAllInfo) _data.get(position);
            vh.tv_userWangWang_Name.setText(info.getAccount());
            vh.tv_time.setText(DateUtils.getDateToStringDay(info.getAddTime()));
            vh.tv_money.setText(DateUtils.formateRate(String.valueOf(info.getBaseCmi())));
            vh.tv2.setText(info.getBackName());
            Picasso.with(mActivity).load(ApiHttpClient.URL2 + info.getImages()).placeholder(R.mipmap.rwcx_jiazaizhong).error(R.mipmap.rwcx_jiazaizhong).into(vh.iv_goods);
        }

        class ViewHolder extends RecyclerBaseAdapter.ViewHolder {
            @BindView(R.id.tv_userWangWang_Name)
            TextView tv_userWangWang_Name;
            @BindView(R.id.tv_time)
            TextView tv_time;
            @BindView(R.id.tv_money)
            TextView tv_money;
            @BindView(R.id.tv1)
            TextView tv1;
            @BindView(R.id.tv2)
            TextView tv2;
            @BindView(R.id.iv_goods)
            ImageView iv_goods;
            @BindView(R.id.iv_userWangWang)
            ImageView iv_userWangWang;

            ViewHolder(int viewType, View view) {
                super(viewType, view);
                ButterKnife.bind(this, view);
            }
        }

    }
}
